package com.graduate.daydayup.fragment.lesson

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.provider.AlarmClock
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.graduate.daydayup.App
import com.graduate.daydayup.R
import com.graduate.daydayup.activity.EditScheduleActivity
import com.graduate.daydayup.activity.LessonAddActivity
import com.graduate.daydayup.activity.LessonSettingAtivity
import com.graduate.daydayup.base.BaseFragment
import com.graduate.daydayup.bean.MySubject
import com.graduate.daydayup.room.MyRoomDataBase
import com.graduate.daydayup.view.MyPickerView
import com.zhuangfei.timetable.listener.ISchedule
import com.zhuangfei.timetable.listener.ISchedule.OnWeekChangedListener
import com.zhuangfei.timetable.listener.IWeekView.OnWeekItemClickedListener
import com.zhuangfei.timetable.listener.IWeekView.OnWeekLeftClickedListener
import com.zhuangfei.timetable.listener.OnItemBuildAdapter
import com.zhuangfei.timetable.listener.OnSlideBuildAdapter
import com.zhuangfei.timetable.model.Schedule
import com.zhuangfei.timetable.model.ScheduleSupport
import kotlinx.android.synthetic.main.activity_lessonset.*
import kotlinx.android.synthetic.main.fragment_lesson.*
import java.util.*
import kotlin.collections.ArrayList

class LessonFragment : BaseFragment(){
    val default="8:20,\n9:15,\n10:15,\n11:10,\n13:00,\n13:55,\n14:50,\n16:45,\n18:30,\n19:25,\n20:20"

    override fun getLayout()= R.layout.fragment_lesson

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun initViewIds(view: View?) {

    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    @SuppressLint("SetTextI18n")
    fun refreshData(){
        //设置周起始日
        if (SPUtils.getInstance().getInt("currentweek")!=-1){
            val timeTransfrom = SPUtils.getInstance().getInt("currentweek")
            tv_cuweek.setText("第${timeTransfrom}周")
        }else tv_cuweek.setText("第1周")
        val all = MyRoomDataBase.getInstance(App.instance!!).scheduleDao().getAll()
        val list = ArrayList<MySubject>()
        if (all != null) {

            for (bean in all){
                val arrayList = ArrayList<Int>()
                val split = bean.weeklist.split(",")
                for (i in split[0].toInt()..split[1].toInt()){
                    arrayList.add(i)
                }
                val mySubject = MySubject("", bean.name, bean.room, bean.teacher, arrayList, bean.start,
                        bean.step, bean.day, 0, "")
                list.add(mySubject)
            }
        }
        var currentweek=1
        if (SPUtils.getInstance().getInt("currentweek")!=-1) {
            currentweek=SPUtils.getInstance().getInt("currentweek")
        }
        val pickview_currentweek= MyPickerView(context)
        var listItemWeek=ArrayList<String>()
        for (i in 1..20){
            listItemWeek.add("第"+i.toString()+"周")
        }

        pickview_currentweek.setPicker(listItemWeek)
        //设置周次选择属性
        mWeekView.curWeek(currentweek)
                .callback(OnWeekItemClickedListener { week ->
                    val cur: Int = mTimetableView.curWeek()
                    //更新切换后的日期，从当前周cur->切换的周week
                    mTimetableView.onDateBuildListener()
                            .onUpdateDate(cur, week)
                    mTimetableView.changeWeekOnly(week)
                })
                .callback(OnWeekLeftClickedListener {
                    pickview_currentweek.show()
                })
                .isShow(true) //设置隐藏，默认显示
                .source(list)
                .showView()
        pickview_currentweek.setOnoptionsSelectListener { options1, option2, options3 ->
            mWeekView.curWeek(options1+1)
            SPUtils.getInstance().put("currentweek",options1+1)
            mWeekView.updateView()
            mTimetableView.updateDateView()
        }
        val slideAdapter = OnSlideBuildAdapter()
        if (SPUtils.getInstance().getString("lessontime")!=null&&!SPUtils.getInstance().getString("lessontime").isEmpty()){
            val replace = SPUtils.getInstance().getString("lessontime").replace("\n", "")
            val split = replace.split(",")
            slideAdapter.times = split.toTypedArray()
            mTimetableView.callback(slideAdapter)
            mTimetableView.updateSlideView()
        }else{
            val replace =default.replace("\n", "")
            val split = replace.split(",")
            slideAdapter.times = split.toTypedArray()
            mTimetableView.callback(slideAdapter)
            mTimetableView.updateSlideView()
        }


        //TODO 设置课程节数
        if (SPUtils.getInstance().getInt("lessoncounts")!=-1){
            mTimetableView.maxSlideItem(SPUtils.getInstance().getInt("lessoncounts"))
        }else{
            mTimetableView.maxSlideItem(10)
        }
        //TODO 设置当前周
        var currentterm=""
        if (SPUtils.getInstance().getString("currentterm")!=null&&!SPUtils.getInstance().getString("currentterm").isEmpty()) {
            currentterm=SPUtils.getInstance().getString("currentterm")
        }
        mTimetableView.curWeek(currentterm)
                .curWeek(currentweek)
                .callback(ISchedule.OnItemClickListener {
                        v, scheduleList ->
                    if (scheduleList!=null&&scheduleList.size>0){
                        val allByDayAndName = MyRoomDataBase.getInstance(App.instance!!).scheduleDao().getAllByDayAndName(
                            scheduleList[0].day,
                            scheduleList[0].name
                        )
                        val intent = Intent(activity, EditScheduleActivity::class.java)
                        intent.putExtra("id",allByDayAndName.id)
                        intent.putExtra("day",scheduleList[0].day+1)
                        intent.putExtra("start",scheduleList[0].start)
                        intent.putExtra("step",scheduleList[0].step)
                        startActivity(intent)
                    }


                })
                .callback(ISchedule.OnFlaglayoutClickListener { day, start ->
                    val intent = Intent(activity, LessonAddActivity::class.java)
                    intent.putExtra("day",day+1)
                    intent.putExtra("start",start)
                    startActivity(intent)
                })
                .callback(ISchedule.OnItemLongClickListener { v, day, start ->

                })
                .callback(OnWeekChangedListener { curWeek -> })
                .callback(object : OnItemBuildAdapter() {
                    override fun onItemUpdate(
                            layout: FrameLayout,
                            textView: TextView,
                            countTextView: TextView,
                            schedule: Schedule,
                            gd: GradientDrawable
                    ) {
                        super.onItemUpdate(layout, textView, countTextView, schedule, gd)

                    }
                })
                .source(list)
                .showView()
        iv_set.setOnClickListener {
            ActivityUtils.startActivity(LessonSettingAtivity::class.java)
        }
    }

    override fun initView(view: View?) {

    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    fun setSystemAlarmClock(context: Context, message: String?, days:Int, hour: Int, minute: Int): Boolean {
        val arrayList = ArrayList<Int>()
        val intent = Intent(AlarmClock.ACTION_SET_ALARM)
        intent.putExtra(AlarmClock.EXTRA_MESSAGE, message)

        intent.putExtra(AlarmClock.EXTRA_HOUR, hour)
        intent.putExtra(AlarmClock.EXTRA_MINUTES, minute)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        return try {
            context.startActivity(intent)
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

}